package com.eway.viewanimator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewAnimationActivity extends AppCompatActivity implements View.OnClickListener {

	private Button alaphBtn, rolateBtn, scaleBtn, transitionBtn, set;
	private TextView showTv;


	@Override

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		alaphBtn = findViewById(R.id.alaph);
		showTv = findViewById(R.id.show);
		rolateBtn = findViewById(R.id.rolate);
		scaleBtn = findViewById(R.id.scale);
		transitionBtn = findViewById(R.id.transition);
		set = findViewById(R.id.set);

		alaphBtn.setOnClickListener(this);
		rolateBtn.setOnClickListener(this);
		scaleBtn.setOnClickListener(this);
		transitionBtn.setOnClickListener(this);
		showTv.setOnClickListener(this);
		set.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.alaph:
				/*使用加载animal中xml文件加载动画效果*/
//				Animation alaphAnimation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.animation_alaph);
//				showTv.startAnimation(alaphAnimation);
				/*使用animation java实现相同的效果*/
				AlphaAnimation alphaAnimationJava = new AlphaAnimation(0, 1f);
				alphaAnimationJava.setDuration(3000);  // 必须设置持续时间
				showTv.startAnimation(alphaAnimationJava);

				break;
			case R.id.rolate:
				/*使用加载animal中xml文件加载动画效果*/
//				Animation rolateAnimation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.animation_rotate);
//				showTv.startAnimation(rolateAnimation);
				/*
				 *
				 * 使用rotateAnimation 实现相同效果
				 * pivotX(Y)Type:相当于xml中的x,y位置的偏移的类型。
				 * 值：对应RotateAnimation.ABSOLUTE
				 * 自身%：对应RotateAnimation.RELATIVE_TO_SELF
				 * %p:对应RotateAnimation.RELATIVE_TO_PARENT
				 * */

				RotateAnimation rotateAnimationJava = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_PARENT, 0.05f, RotateAnimation.RELATIVE_TO_PARENT, 0.05f);
				rotateAnimationJava.setDuration(2000);
				showTv.startAnimation(rotateAnimationJava);
				break;
			case R.id.scale:
				/* 使用加载anim中xml文件加载动画效果*/
//				Animation animation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.animation_scale);
//				showTv.startAnimation(animation);

				/*使用scaleAnimation方法时间相同的效果
				 * pivotX(Y)Type：同上
				 * */
				ScaleAnimation scaleAnimationJava = new ScaleAnimation(0, 2f, 0, 2f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
				scaleAnimationJava.setDuration(2000);
				scaleAnimationJava.setFillAfter(true); // 效果不返回到初始状态
				showTv.startAnimation(scaleAnimationJava);
				break;
			case R.id.transition:
				/*使用anim 中xml文件加载的动画*/
//				Animation transitonAnimation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.animation_translate);
//				showTv.startAnimation(transitonAnimation);
				/*使用translationAnimation实现的相同的动画效果*/
				TranslateAnimation translateAnimationJava= new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.RELATIVE_TO_SELF, 2f,
						TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.RELATIVE_TO_SELF,2f);
				translateAnimationJava.setDuration(5000);
				showTv.startAnimation(translateAnimationJava);
				break;
			case R.id.show:
				/*移动了，但是点击事件还是在原来的位置*/
				Toast.makeText(ViewAnimationActivity.this, "1111", Toast.LENGTH_LONG).show();
				break;


			case R.id.set:
				/*使用anim 中xml文件加载动画*/
//				Animation setAnimation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.animation_set);
//				showTv.startAnimation(setAnimation);

				AnimationSet animationSet=new AnimationSet(true);
				TranslateAnimation translateAnimationSet = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.RELATIVE_TO_SELF, 2f,
						TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.RELATIVE_TO_SELF,2f);
				ScaleAnimation scaleAnimationSet = new ScaleAnimation(0, 2f, 0, 2f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
				RotateAnimation rotateAnimationSet = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_PARENT, 0.05f, RotateAnimation.RELATIVE_TO_PARENT, 0.05f);
				AlphaAnimation alphaAnimationSet = new AlphaAnimation(0, 1f);

				animationSet.addAnimation(translateAnimationSet);
				animationSet.addAnimation(scaleAnimationSet);
				animationSet.addAnimation(rotateAnimationSet);
				animationSet.addAnimation(alphaAnimationSet);
                animationSet.setDuration(4000);

				showTv.startAnimation(animationSet);
				break;


		}
	}
}
